﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Web.Mvc.Ajax;

namespace TemplateWeb.Helpers
{
    public static class GridPagerExtension
    {
        public static string GridPager(this AjaxHelper helper)
        {
            return GridPager(helper, new GridPagerProperties());
        }

        public static string GridPager(this AjaxHelper helper, GridPagerProperties properties)
        {
            return GenerateLinks(helper, properties);
        }

        private static string GenerateLinks(this AjaxHelper helper, GridPagerProperties properties)
        {
            TagBuilder pagerDiv = new TagBuilder("div") { };
            pagerDiv.MergeAttributes(new Dictionary<string, string> { { "class", "pager" } });

            if (properties.PageCount > 1)
            {
                //create the previous tag
                if (properties.CurrentPageIndex > 1)
                {
                    properties.LinkText = "<";
                    properties.RefPageIndex = properties.CurrentPageIndex - 1;
                    //add previous tag
                    pagerDiv.InnerHtml += GetNavItem(helper, properties).ToString();
                }

                for (int page = 1; page <= properties.PageCount; page++)
                {
                    //create paging tags
                    properties.LinkText = page.ToString();
                    properties.RefPageIndex = page;
                    //create tag
                    TagBuilder tag = GetNavItem(helper, properties);
                    if (page == properties.CurrentPageIndex)
                    {
                        tag.Attributes.Add("class", "selected");
                    }

                    TagBuilder label = new TagBuilder("label")
                    {
                        InnerHtml = HttpUtility.HtmlEncode("...")
                    };
                    label.MergeAttributes(new Dictionary<string, string> { { "text", "..." } });


                    if (properties.PageCount > 11)
                    {
                        if (properties.CurrentPageIndex < 7)
                        {
                            //beginning
                            if (page < 9 || ((properties.PageCount - page) < 3))
                                pagerDiv.InnerHtml += tag.ToString();
                            else if (page == 10)
                                pagerDiv.InnerHtml += label.ToString();
                        }
                        else if (properties.CurrentPageIndex >= (properties.PageCount - 7))
                        {
                            //end
                            if (page < 2 || (page >= properties.PageCount - 9))
                                pagerDiv.InnerHtml += tag.ToString();
                            else if (page == 2)
                                pagerDiv.InnerHtml += label.ToString();
                        }
                        else
                        {
                            //middle
                            if (page < 2)
                                pagerDiv.InnerHtml += tag.ToString();
                            else if (page == 2)
                                pagerDiv.InnerHtml += label.ToString();
                            else if ((page >= (properties.CurrentPageIndex - 4)) && (page <= (properties.CurrentPageIndex + 4)))
                                pagerDiv.InnerHtml += tag.ToString();
                            else if (page == properties.CurrentPageIndex + 5)
                                pagerDiv.InnerHtml += label.ToString();
                            else if (page >= properties.PageCount - 2)
                                pagerDiv.InnerHtml += tag.ToString();
                        }
                    }
                    else
                        pagerDiv.InnerHtml += tag.ToString();
                }

                //create the next tag
                if (properties.CurrentPageIndex < properties.PageCount)
                {
                    properties.LinkText = ">";
                    properties.RefPageIndex = properties.CurrentPageIndex + 1;
                    //add previous tag
                    pagerDiv.InnerHtml += GetNavItem(helper, properties).ToString();
                }
            }

            // create the total items tag
            TagBuilder countDiv = new TagBuilder("div")
            {
                InnerHtml = String.Format("{0} items", properties.RecordCount)
            };
            countDiv.MergeAttributes(new Dictionary<string, string> { { "class", "totalrecords" }, { "id", "grid_totalrecords" } });

            pagerDiv.InnerHtml += countDiv.ToString();

            return pagerDiv.ToString();
        }

        public static TagBuilder GetNavItem(this AjaxHelper ajax, GridPagerProperties properties)
        {
            MvcHtmlString htmlTemplate = null;

            switch (properties.TypeOfParam)
            {
                case EnumTypeOfParam.DEFAULT:
                    htmlTemplate = ajax.ActionLink(properties.LinkText, properties.ActionName, properties.Controller,
                                    new { page = properties.RefPageIndex },
                                    new AjaxOptions
                                    {
                                        UpdateTargetId = properties.UpdateTargetId
                                    });
                    break;

                case EnumTypeOfParam.QuanLy:
                    htmlTemplate = ajax.ActionLink(properties.LinkText, properties.ActionName, properties.Controller,
                                    new { role = properties.StrTransferValue, page = properties.RefPageIndex },
                                    new AjaxOptions
                                    {
                                        UpdateTargetId = properties.UpdateTargetId
                                    });
                    break;
            }

            TagBuilder linkTag = new TagBuilder("li")
            {
                InnerHtml = htmlTemplate.ToHtmlString()
            };

            return linkTag;
        }
    }

}
